﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SapNwRfc;
using System.Collections.Generic;
using System.Linq;
using VOL.Core.DBManager;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.Controllers.DjErp
{


    /// <summary>
    /// 
    /// 此方法调用SAP ECC6.0 RFC成功的例子
    /// 条件如下：
    /// 1、dotnet需要安装的nuget SapNwRfc 1.4.0;
    /// 2、dotnet平台net5.0环境支持；
    /// 3、复制nwrfc750P_6-70002755\\nwrfcsdk\lib下的dll文件到windows\system32目录下；
    /// 4、本程序的运维VS2022 
    /// 5、注意SAP的RFC函数的I_MATNR需要创建table type类型的字段，才能接受多条数据输入参数
    /// </summary>
    [Route("api/saptest2")]
    [ApiController]
    public class sapRFC_test : Controller
    {
        [HttpGet, Route("conn_sap2")]
        public dynamic Conn_sap()
        {
            //Trace = 0//不产生日志文件 =8产生日志文件
            string connectionString = "AppServerHost=10.100.8.126; SystemNumber=00; User=c000541; Password=5Tgb6yhn; Client=700; Language=ZH; PoolSize=5; Trace=0";//测试系统
           // string connectionString = "AppServerHost=10.100.8.124; SystemNumber=00; User=c000541; Password=2Wsx3edc; Client=700; Language=ZH; PoolSize=5; Trace=0";//正式系统
            using var connection = new SapConnection(connectionString);
            connection.Connect();
            using var someFunction = connection.CreateFunction("Z_GET_MATERIAL_RFC");
            List<SomeFunctionitem> ils2 = new List<SomeFunctionitem>();
            var result = someFunction.Invoke<SomeFunctionResult>(new SomeFunctionParameters
            {
                I_WERKS = "9000",
                I_MATNR = new matnrs_in[]//对应的SAP字段类型是table type
                {
                   new matnrs_in{ MATNR="000000110022101869" },
                   new matnrs_in{ MATNR="000000190111162500" },
                   new matnrs_in{ MATNR="000000100100100438" },
                },
                
            });
            for (int ii = 0; ii < result.ET_MATL.Length; ii++)
            {
                ils2.Add(result.ET_MATL[ii]);
            }
            List<SomeFunctionitem> outlit = ils2.Where((x, i) => ils2.FindIndex(z => z.MATNR == x.MATNR) == i).ToList();//删除重复的元素
            string[]matnrs= outlit.Select(x => x.MATNR).ToArray();
            var removedb = DBServerProvider.DbContext.Set<sap_mara>();
            IQueryable<sap_mara> _query = removedb.Where(t => matnrs.Contains(t.matnr)).AsQueryable();
           // IQueryable<sap_mara> _query = removedb.Where(t => outlit.Any(o=>o.MATNR==t.matnr)).AsQueryable();
            removedb.RemoveRange(_query);
            DBServerProvider.DbContext.SaveChanges();

            for (int i = 0; i < outlit.Count; i++)
            {
                sap_mara sap_mara = new sap_mara();
                sap_mara.matnr = outlit[i].MATNR;
                sap_mara.maktx = outlit[i].MAKTX;
                removedb.Update(sap_mara);
            }
            DBServerProvider.DbContext.SaveChanges();
            return outlit;
        }

        /// <summary>
        /// 输入参数
        /// </summary>
        class SomeFunctionParameters
        {
            [SapName("I_WERKS")]
            public string I_WERKS { get; set; }
            [SapName("I_MATNR")]
            public matnrs_in[] I_MATNR { get; set; }
        }
        /// <summary>
        /// 输出参数
        /// </summary>
        class SomeFunctionResult
        {
            [SapName("ET_MATL")]
            public SomeFunctionitem[] ET_MATL { get; set; }
            
           
        }

        /// <summary>
        /// 对应的SAP中返回的结构
        /// </summary>
        public class SomeFunctionitem
        {
            //[SapName("MATNR")]//可以不用
            public string MATNR { get; set; }
            //[SapName("WERKS")]
            public string WERKS { get; set; }
            // [SapName("MAKTX")]
            public string MAKTX { get; set; }

        }
        class matnrs_in
        {
            [SapName("MATNR")]
            public string MATNR { get; set; }
        }
    }
}
